
class Solution {
public:
    int toInt(string a)//转换成int型的函数
    {
        int res=0;
        for(int i=0;i<a.length();i++)
        {
            res*=10;
            res+=a[i]-48;
        }
        return res;
    }
    int calculate(string s) {
        /*1先将中缀表达式转换为后缀式
        2.计算后缀表达式的值
        */
        vector<string>back;//后缀表达式
        /*转换后缀式*/
        stack<char>a;
        for(int i=0;i<s.length();i++)
        {
            char t=s[i];
            if(t=='+')
            {
                while(a.empty()==0)
                {
                    string t3=""; 
                    char t4=a.top();
                    t3+=t4;
                    back.push_back(t3);
                    a.pop();
                }
                a.push('+');
            }
            else if(t=='-')
            {
                  while(a.empty()==0)
                {
                   string t3=""; 
                    char t4=a.top();
                    t3+=t4;
                    back.push_back(t3);
                    a.pop();
                }
                a.push('-');
            }
            else if(t=='*')
            {
                  while(a.empty()==0)
                {
                    
                    char t4=a.top();
                   
                    if(t4=='+'||t4=='-')
                    {
                        break;
                    }
                    string t3=""; 
                    t3+=t4;
                    back.push_back(t3);
                    a.pop();
                }
                a.push('*');
            }
            else if(t=='/')
            {
                  while(a.empty()==0)
                {
                    
                    char t4=a.top();
                    
                    if(t4=='+'||t4=='-')
                    {
                        break;
                    }
                    string t3=""; 
                    t3+=t4;
                    back.push_back(t3);
                    a.pop();
                }
                a.push('/');
            }
            else if(t==' ')
            {

            }
            else//数字
            {
                string t1="";
                while(i<s.length())
                {
                    char t2=s[i];
                    if(t2=='+'||t2=='-'||t2=='*'||t2=='/'||t2==' ')
                    {
                        //i--;
                        break;
                    }
                    else{
                        t1+=t2;
                        i++;
                    }
                }
                i--;//回溯一个单位，找到运算符号
                back.push_back(t1);
            }
        }
    while(a.empty()==0)//把栈里剩余的符号拿出来，添加到后缀表达式里
    {
        char t=a.top();
        string t1="";
        t1+=t;
        back.push_back(t1);
        a.pop();
    }
    /*后缀表达式的计算*/
    stack<int>b;
    for(int i=0;i<back.size();i++)
    {
        char t=back[i][0];
        if(t=='+')
        {
            int c1,c2,c3;
            c1=b.top();
            b.pop();
            c2=b.top();
            b.pop();
            c3=c2+c1;
            b.push(c3);

        }
        else if(t=='-')
        {
            int c1,c2,c3;
            c1=b.top();
            b.pop();
            c2=b.top();
            b.pop();
            c3=c2-c1;
            b.push(c3);

        }
        else if(t=='*')
        {
             int c1,c2,c3;
            c1=b.top();
            b.pop();
            c2=b.top();
            b.pop();
            c3=c2*c1;
            b.push(c3);

        }
        else if(t=='/')
        {
             int c1,c2,c3;
            c1=b.top();
            b.pop();
            c2=b.top();
            b.pop();
            c3=c2/c1;
            b.push(c3);

        }
        else{//数字
        b.push(toInt(back[i]));
        }
    }
        return b.top();

    }
};